var testEditor;
$(function () {
    testEditor = editormd("bbseditormd", {
        width: "100%",
        height: "100%",
        syncScrolling: "single",
        path: "/editormd/lib/",

        // theme: "dark",
        // previewTheme: "dark",
        // editorTheme: "pastel-on-dark",
        // markdown: md,
        codeFold: true,
        //syncScrolling : false,
        saveHTMLToTextarea: true,    // 保存 HTML 到 Textarea  这个配置，方便post提交表单
        searchReplace: true,
        //watch : false,                // 关闭实时预览
        htmlDecode: "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
        // toolbar  : false,             //关闭工具栏
        //previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮，默认开启
        emoji: true,
        taskList: true,
        tocm: true,         // Using [TOCM]
        tex: true,                   // 开启科学公式TeX语言支持，默认关闭
        flowChart: true,             // 开启流程图支持，默认关闭
        sequenceDiagram: true,       // 开启时序/序列图支持，默认关闭,
        //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
        //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
        //dialogDraggable : false,    // 设置弹出层对话框不可拖动，全局通用，默认为true
        //dialogMaskOpacity : 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
        //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff

        //启动本地图片上传功能
        imageUpload: true,
        watch: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        imageUploadURL: "/uploadjpg",//后端的上传图片服务地址
        onload: function () {
            //console.log('onload', this);
            //this.fullscreen();
            //this.unwatch();
            //this.watch().fullscreen();

            //this.setMarkdown("#PHP");
            //this.width("100%");
            //this.height(480);
            //this.resize("100%", 640);
        }
    });
});
$("#savebtn").click(function () {
    add_art(0);
})
$("#publishbtn").click(function () {
    add_art(1);
})


function add_art(r) {
    var userid = $("#userart").val();
    var diseasetagid = $("#diseasetag").val();
    var title = $("#title").val();
    var description = $("#description").val();
    var content = testEditor.getHTML();
    // alert(content)
    $.ajax({
        type: 'post',
        url: '/article/addart',
        contentType: "application/json",
        data: JSON.stringify({
            userid: userid,
            diseaseid: diseasetagid,
            titile: title,
            img: imgpath,
            description: description,
            content: content,
            visable: r
        }),
        success: function (rescode) {
            if (rescode == "200") {
                swal({
                    title: "添加成功!",
                    text: "2秒后自动跳转",
                    timer: 2000,
                    showConfirmButton: false
                })
                window.location.href = "/article/index"
            }
        },
        error: function () {
            swal("修改失败", "error")
        }
    });
    imgpath = ''
}

var imgpath = ''

function uploadAvator() {
    $.ajax({
        type: 'post',
        url: '/upload/artavator',
        data: new FormData($('#uploadArtForm')[0]),
        processData: false,
        contentType: false,
        success: function (rescode) {
            if (rescode != '500') {
                $("#loadbtn").css("background-color", "green").val("sdfsf").css("color", "#ffffff");
                imgpath = rescode;
            } else {
                $("#loadbtn").css("background-color", "#e07474").css("color", "#ffffff");
            }
        },
        error: function () {
            $("#loadbtn").css("background-color", "#e07474").css("color", "#ffffff");
        }
    });
}
